package com.feihao.zhou;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Examination1 {
    public static void main(String[] args) throws IOException {
        //读取本地的文本文件：（中英翻译.txt）:
        BufferedReader br = new BufferedReader(
                new FileReader("F:\\Download\\笔记\\day08\\考试\\试题A\\中英翻译.txt"));

        //创建Map集合存储字符数组的元素：
        TreeMap<Character, Integer> map = new TreeMap<>();
        String len;
        while ((len = br.readLine()) != null) {
            //将字符串转为字符数组：
            char[] chars = len.toCharArray();

            //遍历数组加入Map集合：
            for (char aChar : chars) {
                map.put(aChar, map.containsKey(aChar) ? map.get(aChar) + 1 : 1);
            }
        }
        br.close();

        /*System.out.println(map);*/
        //遍历map中的数据，存储到ArrayList中：
        ArrayList<String> list = new ArrayList<>();
        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            Character key = entry.getKey();
            Integer value = entry.getValue();
            String valueString = "(" + value + ")";
            //组装好后添加到list
            list.add(key + valueString);
        }
        /*for (String s : list) {
            System.out.println(s);
        }*/
        /**
         * 程序中统计完后，将统计结果在控制台输出，输出效果为下图，每行输出5个，换一行；
         */
        System.out.println("排序前：");
        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            count++;
            System.out.print(list.get(i) + "\t\t");
            if (count % 5 == 0) {
                System.out.println();
            }
        }


        /**
         * 最后再将统计内容，写入到另一个文件中（统计.txt），必须有序，按照【字符数量】排序【升序】；
         */
        //将map中的元素自然组合添加到list中： 元素如：！6     行不通！！！！
       /* ArrayList<String> listNew = new ArrayList<>();
        for (Map.Entry<Character, Integer> entry : entries) {
            Character key = entry.getKey();
            Integer value = entry.getValue();
            listNew.add(key+value.toString());
        }

        //将新集合进行排序：
        listNew.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.charAt(1) - o2.charAt(1);
            }
        });*/

        //在map上做文章：  通过map转换为stream流后进行排序，再转换为list
        List<Map.Entry<Character, Integer>> listNew = map.entrySet().stream().sorted(new Comparator<Map.Entry<Character, Integer>>() {
            @Override
            public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                return o1.getValue() - o2.getValue();
            }
        }).collect(Collectors.toList());

        //将排序好的集合写入文件：
        BufferedWriter bw = new BufferedWriter(new FileWriter(
                "D:\\IdeaProjects\\Job-code\\EXEMP\\src\\com\\feihao\\zhou\\排序后.txt"));
        int count1 = 0;
        for (int i = 0; i < listNew.size(); i++) {
            count1++;
            Map.Entry<Character, Integer> entry = listNew.get(i);
            Character key = entry.getKey();
            Integer value = entry.getValue();
            bw.write(key+"("+value+")");
            bw.write("\t\t");
            if (count1 % 5 == 0) {
                bw.newLine();
            }
            bw.flush();
        }

        bw.close();

    }
}
